﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using ExcelDna.Integration;
using FinPlusStructure;
using FinPlusCommon;
using u = FinPlusUtility.Utility;
using s = FinPlusStructure.FinPlusStructure;

namespace FinPlusDNA
{
    public class FinPlusDNA
    {
        //QauntLib wrappers
        static ql ql = new ql();
        #region date

        [ExcelFunction(Description = "gets next biz date", Category = "FinPlus.Date")]
        public static object NextBizDate(DateTime Date, string Holidays)
        {
            try
            {
                 return ql.NextBizDate((int)Date.ToOADate(), Holidays);
            }
            catch (Exception e)
            {
                 return e.ToString();
            } 
        }

        [ExcelFunction(Description = "gets next biz date", Category = "FinPlus.Date")]
        public static object NextBizDate(DateTime Date, string Period, string BizDayConv, bool EndOfMonth, string Holidays)
        {
            try
            {
                return ql.NextBizDate((int)Date.ToOADate(), Period, BizDayConv, EndOfMonth, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "gets next imm date", Category = "FinPlus.Date")]
        public static object NextImmDate(DateTime Date)
        {
            try
            {
                 return ql.NextImmDate((int)Date.ToOADate());
            }
            catch (Exception e)
            {
                 return e.ToString();
            } 
        }

        #endregion

        #region engine related

        [ExcelFunction(Description = "values trade", Category = "FinPlus.Analytics")]
        public static object MarketSet(string MarketName, DateTime TodaysDate)
        {
            try
            {
                ql.MarketSet(MarketName, (int)TodaysDate.ToOADate());
                return MarketName;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "values trade", Category = "FinPlus.Analytics")]
        public static object MarketValueTrade(string Market, string CacheName, string TradeId, string ControlString, string MakeVolatile)
        {
            try
            {
                 return ql.MarketValueTrade(Market, CacheName, TradeId, ControlString);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "values trades", Category = "FinPlus.Analytics")]
        public static object MarketValueTrades(string Market, string CacheName, string ControlString, string MakeVolatile)
        {
            try
            {
                return ql.MarketValueTrades(Market, CacheName, ControlString);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        #endregion

        #region pricing structures

        [ExcelFunction(Description = "builds indexs", Category = "FinPlus.Analytics")]
        public static object SetIndex(string MarketName, string CurveName, string IndexName, string IndexType, string Tenor, string Fixings)
        {
            try
            {
                return ql.Index(MarketName, CurveName, IndexName, IndexType, Tenor, Fixings);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds yield curve", Category = "FinPlus.Analytics")]
        public static object YieldCurve(string MarketName, string CurveName, string DiscountCurveName, DateTime SettlementDate, string Rates, string DayCount, double Tolerance, string Holidays)
        {
            try
            {
                 return ql. YieldCurve(MarketName, CurveName, DiscountCurveName, (int)SettlementDate.ToOADate(), Rates, DayCount, Tolerance, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds default curve", Category = "FinPlus.Analytics")]
        public static object DefaultCurve(string MarketName, string CurveName, string DiscountCurveName, DateTime SettlementDate, string Spreads, double RecoveryRate, string DayCount, string Holidays)
        {
            try
            {
                return ql.DefaultCurve(MarketName, CurveName, DiscountCurveName, (int)SettlementDate.ToOADate(), Spreads, RecoveryRate,  DayCount, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds vol surf", Category = "FinPlus.Analytics")]
        public static object VolSurf(string MarketName, string CurveName, string VolSurfName, string Vols, string Index, string Model)
        {
            try
            {
                return ql.VolSurf(MarketName, CurveName, VolSurfName, Vols, Index, Model);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds flat forward yield curve", Category = "FinPlus.Analytics")]
        public static object FlatForwardCurve(string MarketName, string CurveName, string DiscountCurveName, DateTime SettlementDate, int Rate, string DayCount)
        {
            try
            {
                return ql.FlatForwardCurve(MarketName, CurveName, DiscountCurveName, (int)SettlementDate.ToOADate(), Rate, DayCount);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds flat forward yield curve", Category = "FinPlus.Analytics")]
        public static object FlatForwardCurve(string MarketName, string CurveName, string DiscountCurveName, DateTime SettlementDate, int Rate, string DayCount, string CpnFrq, string Compounded)
        {
            try
            {
                return ql.FlatForwardCurve(MarketName, CurveName, DiscountCurveName, (int)SettlementDate.ToOADate(), Rate, DayCount, CpnFrq, Compounded);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds bond curve", Category = "FinPlus.Analytics")]
        public static object BondCurve(string MarketName, string CurveName, string DiscountCurveName, int SettlementDate, string Rates, string DayCount, double Tolerance, string Holidays) 
        {
            try
            {
                return ql.BondCurve(MarketName, CurveName, DiscountCurveName, SettlementDate, Rates, DayCount, Tolerance, Holidays); 
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds depo rate", Category = "FinPlus.Analytics")]
        public static object DepoRate(string MarketName, string RateName, double Rate, string Tenor, int FixingDays, string DayCount, string BizConv, string Holidays)
        {
            try
            {
                return ql.DepoRate(MarketName, RateName, Rate, Tenor, FixingDays, DayCount, BizConv, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds fra rate", Category = "FinPlus.Analytics")]
        public static object FraRate(string MarketName, string RateName, double Rate, int Start, int End, int FixingDays, string DayCount, string BizConv, string Holidays)
        {
            try
            {
                return ql.FraRate(MarketName, RateName, Rate, Start, End, FixingDays, DayCount, BizConv, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds future rate", Category = "FinPlus.Analytics")]
        public static object FutRate(string MarketName, string RateName, double Rate, DateTime StartDate, int FutMonths, string DayCount, string BizConv, string Holidays)
        {
            try
            {
                return ql.FutRate(MarketName, RateName, Rate, (int)StartDate.ToOADate(), FutMonths, DayCount, BizConv, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds swap rate", Category = "FinPlus.Analytics")]
        public static object SwapRate(string MarketName, string RateName, double Rate, string Tenor, int FixingDays, int ForwardStart, string FloatLegIndex, string FixedLegFreq, string FixedLegDayCount, string FixedLegBizConv, string Holidays)
        {
            try
            {
                return ql.SwapRate(MarketName, RateName, Rate, Tenor, FixingDays, ForwardStart, FloatLegIndex, FixedLegFreq, FixedLegDayCount, FixedLegBizConv, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds cds spread", Category = "FinPlus.Analytics")]
        public static object CdsSpread(string MarketName, string RateName, double Spread, double RecoveryRate, string Tenor, int FixingDays, string DicountCurveName, string Freq, string DayCount, string BizConv, string Holidays)
         {
             try
            {
                return ql.CdsSpread(MarketName, RateName, Spread, RecoveryRate, Tenor, FixingDays, DicountCurveName, Freq, DayCount, BizConv, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds fixed bond", Category = "FinPlus.Analytics")]
        public static object FixedRateBondBuild(string MarketName, string Instrument, string CurveName, string ISIN, string SEDOL, string CUSIP, string BBIdent, double Coupon, double MktPrice, string CouponFrq, double Redemption, int SettlementDays, DateTime EffectiveDate, DateTime TerminationDate, string DayCount, string PayConv, string TermConv, string BondConv, string Holidays)
        {
            try
            {
                //not used by ql proxy but stored for quering/reference via book ISIN, SEDOL, CUSIP and BBIdent,
                return ql.FixedRateBondBuild(MarketName, Instrument, Coupon, MktPrice, CouponFrq, Redemption, SettlementDays, (int)EffectiveDate.ToOADate(), (int)TerminationDate.ToOADate(), DayCount, PayConv, TermConv, BondConv, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }
        
        [ExcelFunction(Description = "builds floating bond", Category = "FinPlus.Analytics")]
        public static object FloatingRateBondBuild(string MarketName, string Instrument, string CurveName, string ISIN, string SEDOL, string CUSIP, string BBIdentity, double Spread, string Curve, string Index, string CouponFrq, double Redemption, int SettlementDays, DateTime EffectiveDate, DateTime TerminationDate, string DayCount, string PayConv, string TermConv, string BondConv, string Holidays)
        {
            try
            {
                //not used by ql proxy but stored for quering/reference via book ISIN, SEDOL, CUSIP and BBIdent,
                return ql.FloatingRateBondBuild(MarketName, Instrument, Spread, Curve, Index, CouponFrq, Redemption, SettlementDays, (int)EffectiveDate.ToOADate(), (int)TerminationDate.ToOADate(), DayCount, PayConv, TermConv, BondConv, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds zero coupon bond", Category = "FinPlus.Analytics")]
        public static object ZeroCouponBondBuild(string MarketName, string Instrument, string CurveName, string ISIN, string SEDOL, string CUSIP, string BBIdentity, double Redemption, int SettlementDays, DateTime EffectiveDate, DateTime TerminationDate, string BondConv, string Holidays)
        {
            try
            {
                //not used by ql proxy but stored for quering/reference via book ISIN, SEDOL, CUSIP and BBIdent,
                return ql.ZeroCouponBondBuild(MarketName, Instrument, Redemption, SettlementDays, (int)EffectiveDate.ToOADate(), (int)TerminationDate.ToOADate(), BondConv, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        #endregion

        #region instruments
        //rate
        [ExcelFunction(Description = "builds vanilla swap", Category = "FinPlus.Analytics")]
        public static object VanillaSwap(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string CurveName, double Nominal, DateTime Start, DateTime Maturity, double FixedRate, string PayRec, string Index, double Spread, string FixLegFrq, string FltLegFrq, string FixLegConv, string FltLegConv, string FixLegDayCount, string FltLegDayCount, string Holidays)
        {
            //same function used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                return ql.VanillaSwap(MarketName, CacheName, TempId, CurveName, Nominal, (int)Start.ToOADate(), (int)Maturity.ToOADate(), FixedRate, PayRec, Index, Spread, FixLegFrq, FltLegFrq, FixLegConv, FltLegConv, FixLegDayCount, FltLegDayCount, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds basis swap", Category = "FinPlus.Analytics")]
        public static object BasisSwap(string Book, string Counterparty, string MarketName, string CacheName, string TempId, double Nominal, DateTime Start, DateTime Maturity, string PayRec, string CurveName, string Index, double Spread, string FltLegFrq, string FltLegConv, string FltLegDayCount, string CurveName2, string Index2, double Spread2, string FltLegFrq2, string FltLegConv2, string FltLegDayCount2, string Holidays)
        {
            //same function used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                return ql.BasisSwap(MarketName, CacheName, TempId, Nominal, (int)Start.ToOADate(), (int)Maturity.ToOADate(), PayRec,
                    CurveName, Index, Spread, FltLegFrq, FltLegConv, FltLegDayCount,
                    CurveName2, Index2, Spread2, FltLegFrq2, FltLegConv2, FltLegDayCount2, 
                    Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds floating leg", Category = "FinPlus.Analytics")]
        public static object FloatingLeg(string Book, string Counterparty, string MarketName, string CacheName, string TempId, double Nominal, DateTime Start, DateTime Maturity, string PayRec, string CurveName, string Index, double Spread, string FltLegFrq, string FltLegConv, string FltLegDayCount, string Holidays)
        {
            //same function used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                return ql.FloatingLeg(MarketName, CacheName, TempId, Nominal, (int)Start.ToOADate(), (int)Maturity.ToOADate(), PayRec,
                    CurveName, Index, Spread, FltLegFrq, FltLegConv, FltLegDayCount,
                    Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds fixed leg", Category = "FinPlus.Analytics")]
        public static object FixedLeg(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string CurveName, double Nominal, DateTime Start, DateTime Maturity, double FixedRate, string PayRec, string Index, string FixLegFrq, string FixLegConv, string FixLegDayCount, string Holidays)
        {
            //same function used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                return ql.FixedLeg(MarketName, CacheName, TempId, CurveName, Nominal, (int)Start.ToOADate(), (int)Maturity.ToOADate(), FixedRate, PayRec, Index,  FixLegFrq, FixLegConv, FixLegDayCount, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds fra", Category = "FinPlus.Analytics")]
        public static object ForwardRateAgreement(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string CurveName, double Nominal, DateTime Start, DateTime Maturity, double FixedRate, string PayRec, string Index)
        {
            //same function used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                return ql.ForwardRateAgreement(MarketName, CacheName, TempId, CurveName, Nominal, (int)Start.ToOADate(), (int)Maturity.ToOADate(), FixedRate, PayRec, Index);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        //rate options
        [ExcelFunction(Description = "builds swaption", Category = "FinPlus.Analytics")]
        public static object Swaption(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string CurveName, string VolSurfName, double Nominal, DateTime Start, DateTime Maturity, double Strike, string PayRec, string Index, double Spread, string FixLegFrq, string FltLegFrq, string FixLegConv, string FltLegConv, string FixLegDayCount, string FltLegDayCount, string Holidays)
        {
            //same function used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                return ql.Swaption(MarketName, CacheName, TempId, CurveName, VolSurfName, Nominal, (int)Start.ToOADate(), (int)Maturity.ToOADate(), Strike, PayRec, Index, Spread, FixLegFrq, FltLegFrq, FixLegConv, FltLegConv, FixLegDayCount, FltLegDayCount, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds bermudanswaption", Category = "FinPlus.Analytics")]
        public static object BermudanSwaption(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string CurveName, string VolSurfName, double Nominal, DateTime Start, DateTime Maturity, double Strike, string PayRec, string Index, double Spread, string FixLegFrq, string FltLegFrq, string FixLegConv, string FltLegConv, string FixLegDayCount, string FltLegDayCount, string Holidays)
        {
            //same function used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                return ql.BermudanSwaption(MarketName, CacheName, TempId, CurveName, VolSurfName, Nominal, (int)Start.ToOADate(), (int)Maturity.ToOADate(), Strike, PayRec, Index, Spread, FixLegFrq, FltLegFrq, FixLegConv, FltLegConv, FixLegDayCount, FltLegDayCount, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        //credit
        [ExcelFunction(Description = "builds cds", Category = "FinPlus.Analytics")]
        public static object CreditDefaultSwap(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string CurveName, double QuotedSpread, string ProtSellBuy, double Nominal, DateTime Start, DateTime Maturity, string PremFreq, string PremConv, string PayConv, string DayCount, string Holidays)
        {
            //same function used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                return ql.CreditDefaultSwap(MarketName, CacheName, TempId, CurveName, QuotedSpread, ProtSellBuy, Nominal, (int)Start.ToOADate(), (int)Maturity.ToOADate(), PremFreq, PremConv, PayConv, DayCount, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        //bond
        [ExcelFunction(Description = "builds fixed rate bond", Category = "FinPlus.Analytics")]
        public static object FixedRateBond(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string BondName, string CurveName, double Nominal, DateTime Settlement, double Price)
        {
            //same function used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                return ql.FixedRateBond(MarketName, CacheName, TempId, BondName, CurveName, Nominal, (int)Settlement.ToOADate(), Price);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds floating rate bond", Category = "FinPlus.Analytics")]
        public static object FloatingRateBond(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string BondName, string CurveName, double Nominal, DateTime Settlement, double Price)
        {
            try
            {
                return ql.FloatingRateBond(MarketName, CacheName, TempId, BondName, CurveName, Nominal, (int)Settlement.ToOADate(), Price);
            }
            catch (Exception e)
            {
                return e.ToString();
            }

        }

        [ExcelFunction(Description = "builds zero coupon bond", Category = "FinPlus.Analytics")]
        public static object ZeroCouponBond(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string BondName, string CurveName, double Nominal, DateTime Settlement, double Price)
        {
            try
            {
                return ql.ZeroCouponBond(MarketName, CacheName, TempId, BondName, CurveName, Nominal, (int)Settlement.ToOADate(), Price);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "repo fixed bond", Category = "FinPlus.Analytics")]
        public static object Repo(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string BondName, string RepoCurveName, string BondCurveName, double Strike, double Nominal, DateTime SettlementDate, DateTime DeliveryDate, int SettlementDays, string RepoConv, string BondConv, string Holidays)
        {
            try
            {
                return ql.Repo(MarketName, CacheName, TempId, BondName, RepoCurveName, BondCurveName, Strike, Nominal, (int)SettlementDate.ToOADate(), (int)DeliveryDate.ToOADate(), SettlementDays, RepoConv, BondConv, Holidays);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        #endregion

        //info
        #region info

        [ExcelFunction(Description = "gets function list", Category = "FinPlus.Info")]
        public static object FunctionList(string functionName)
        {
            try
            {
                var list = new List<string>();
                MethodInfo[] mi = typeof(FinPlusDNA).GetMethods(BindingFlags.Public | BindingFlags.Static);
                foreach (var m in mi) list.Add(m.Name);
                list.Sort();
                return u.ArrayTranspose(list.ToArray());
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "gets function info", Category = "FinPlus.Info")]
        public static object FunctionFields(string functionName)
        {
            try
            {
                var list = new List<string>();

                //add core items
                list.Add("Id");
                list.Add("FunctionName");
                list.Add("TimeStamp");
                var method = typeof(FinPlusDNA).GetMethod(functionName);
                var parms = method.GetParameters();
                foreach (var parm in parms) list.Add(parm.Name);

                return u.ArrayTranspose(list.ToArray());

            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        #endregion

        //finplus untility wrappers
        #region finplusutility
        [ExcelFunction(Description = "gets string from array 2d", Category = "FinPlus.Parsing")]
        public static string StringFromArray2d(object[,] array, string delimiterRow, string delimiterCol)
        {
            try
            {
                return u.StringFromArray2d(array, delimiterRow == "" ? ';' : char.Parse(delimiterRow), delimiterCol == "" ? ',' : char.Parse(delimiterCol));
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "converts string to array 2d", Category = "FinPlus.Parsing")]
        public static object StringToArray2d(string val, string delimiterRow, string delimiterCol)
        {
            try
            {
                return u.StringToArray2d(val, delimiterRow == "" ? ';' : char.Parse(delimiterRow), delimiterCol == "" ? ',' : char.Parse(delimiterCol));
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "converts string with info to object", Category = "FinPlus.Parsing")]
        public static object StringWithInfoToObject(string str, string infoType)
        {
            try
            {
                return u.StringWithInfoToObject(str, infoType == string.Empty ? "String" : infoType);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        #endregion

        //finplus struture wrappers
        #region finplusstructure
        
        //permissions
        [ExcelFunction(Description = "finplusstructure function Permission", Category = "FinPlus.Permission")]
        public static object Permission(string User, string Name, string ItemName, string ItemValue, bool CanRead, bool CanWrite) { s.Permission(User, Name, ItemName, ItemValue, CanRead, CanWrite); return User; }

        //adaptors
        [ExcelFunction(Description = "finplusstructure QuoteAdaptor", Category = "FinPlus.Adaptor")]
        public static object QuoteAdaptor(string QuoteSource, Function ReceiveAction, Function PublishAction) { s.QuoteAdaptor(QuoteSource, ReceiveAction, PublishAction); return QuoteSource; }

        [ExcelFunction(Description = "finplusstructure TradeAdaptor", Category = "FinPlus.Adaptor")]
        public static object TradeAdaptor(string InstrumentType, Function ReceiveQuoteAction, Function ParseTradeAction, Function ResponseAction) { s.TradeAdaptor(InstrumentType, ReceiveQuoteAction, ParseTradeAction, ResponseAction); return InstrumentType; }

        [ExcelFunction(Description = "finplusstructure Index", Category = "FinPlus.MarketLevel")]
        public static object Index(string Market, string Instrument, string BuySell, double Level, string Source, int Date) { s.Index(Market, Instrument, BuySell, Level, Source, Date); return Level; }

        [ExcelFunction(Description = "finplusstructure Price", Category = "FinPlus.MarketLevel")]
        public static object Price(string Market, string Instrument, string BuySell, double Level, string Source, int Date) { s.Price(Market, Instrument, BuySell, Level, Source, Date); return Level; }

        [ExcelFunction(Description = "finplusstructure Rate", Category = "FinPlus.MarketLevel")]
        public static object Rate(string Market, string Instrument, string BuySell, double Level, string Source, int Date) { s.Rate(Market, Instrument, BuySell, Level, Source, Date); return Level; }

        [ExcelFunction(Description = "finplusstructure Spread", Category = "FinPlus.MarketLevel")]
        public static object Spread(string Market, string Instrument, string BuySell, double Level, string Source, int Date) { s.Spread(Market, Instrument, BuySell, Level, Source, Date); return Level; }

        [ExcelFunction(Description = "finplusstructure Setting", Category = "FinPlus.MarketLevel")]
        public static object Setting(string IndexName, double Level, int Date) { s.Setting(IndexName, Level, Date); return IndexName; }

        //market making
        [ExcelFunction(Description = "finplusstructure Quote", Category = "FinPlus.Structure")]
        public static object Quote(string QuoteRef, string Instrument, string BuySell, double Quote, double Size, string QuoteStyle, string QuoteType, string RateType, string QuoteSource, string Counterparty) { s.Quote(QuoteRef, Instrument, BuySell, Quote, Size, QuoteStyle, QuoteType, RateType, QuoteSource, Counterparty); return QuoteRef; }

        //market making structure
        [ExcelFunction(Description = "finplusstructure QuoteStyle", Category = "FinPlus.Structure")]
        public static object QuoteStyle(string QuoteStyle) { s.QuoteStyle(QuoteStyle); return QuoteStyle; }

        [ExcelFunction(Description = "finplusstructure QuoteType", Category = "FinPlus.Structure")]
        public static object QuoteType(string QuoteType) { s.QuoteType(QuoteType); return QuoteType; }

        [ExcelFunction(Description = "finplusstructure QuoteSource", Category = "FinPlus.Structure")]
        public static object QuoteSource(string QuoteSource, string User, string Location) { s.QuoteSource(QuoteSource, User, Location); return QuoteSource; }

        [ExcelFunction(Description = "finplusstructure QuoteInterets", Category = "FinPlus.Structure")]
        public static object QuoteInterest(string Instrument, string Book, bool IsAxe, string BuySell, double Quote, double Size, string QuoteStyle, string QuoteType, string QuoteMark, string RateType, string Trader) { s.QuoteInterest(Instrument, Book, IsAxe, BuySell, Quote, Size, QuoteStyle, QuoteType, QuoteMark, RateType, Trader); return Instrument; }

        [ExcelFunction(Description = "finplusstructure Instrument", Category = "FinPlus.Structure")]
        public static object Instrument(string Instrument, string Ccy, string LinkedCurve, string InstrumentName, string InstrumentType, string RateType, double ChgMin, double SizeMin, double SizeMax, double ShockUnit) { s.Instrument(Instrument, Ccy, LinkedCurve, InstrumentName, InstrumentType, RateType, ChgMin, SizeMin, SizeMax, ShockUnit); return Instrument; }      

        [ExcelFunction(Description = "finplusstructure InstrumentSpread", Category = "FinPlus.Structure")]
        public static object InstrumentSpread(string Instrument, string SpreadName, string Spread, string SpreadType, bool IsMktToMkt, double BuyAdj, double SellAdj, double Weight, string Trader) { s.InstrumentSpread(Instrument, SpreadName, Spread, SpreadType, IsMktToMkt, BuyAdj, SellAdj, Weight, Trader); return Instrument; }

        [ExcelFunction(Description = "finplusstructure InstrumentType", Category = "FinPlus.Structure")]
        public static object InstrumentType(string InstrumentType) { s.InstrumentType(InstrumentType); return InstrumentType; }

        [ExcelFunction(Description = "finplusstructure RateType", Category = "FinPlus.Structure")]
        public static object RateType(string RateType) { s.InstrumentType(RateType); return RateType; }

        [ExcelFunction(Description = "finplusstructure QuoteMark", Category = "FinPlus.Structure")]
        public static object QuoteMark(string QuoteMark) { s.InstrumentType(QuoteMark); return QuoteMark; }

        [ExcelFunction(Description = "finplusstructure Ccy", Category = "FinPlus.Structure")]
        public static object Ccy(string Ccy, string Name) { s.Ccy(Ccy, Name); return Ccy; }

        [ExcelFunction(Description = "finplusstructure BuySell", Category = "FinPlus.Structure")]
        public static object BuySell(string BuySell) { s.BuySell(BuySell); return BuySell; }

        [ExcelFunction(Description = "finplusstructure SpreadType", Category = "FinPlus.Structure")]
        public static object SpreadType(string SpreadType) { s.SpreadType(SpreadType); return SpreadType; }

        [ExcelFunction(Description = "finplusstructure BizConv", Category = "FinPlus.Structure")]
        public static object BizConv(string BizConv) { s.BizConv(BizConv); return BizConv; }

        [ExcelFunction(Description = "finplusstructure DayCount", Category = "FinPlus.Structure")]
        public static object DayCount(string DayCount) { s.DayCount(DayCount); return DayCount; }

        [ExcelFunction(Description = "finplusstructure Holidays", Category = "FinPlus.Structure")]
        public static object Holidays(string Holidays) { s.Holidays(Holidays); return Holidays; }

        [ExcelFunction(Description = "finplusstructure Frequency", Category = "FinPlus.Structure")]
        public static object Freq(string Freq) { s.Freq(Freq); return Freq; }

        [ExcelFunction(Description = "finplusstructure PayRec", Category = "FinPlus.Structure")]
        public static object PayRec(string PayRec) { s.PayRec(PayRec); return PayRec; }

        [ExcelFunction(Description = "finplusstructure CurveName", Category = "FinPlus.Structure")]
        public static object CurveName(string CurveName, string CurveType) { s.CurveName(CurveName, CurveType); return CurveName; }

        [ExcelFunction(Description = "finplusstructure CurveName", Category = "FinPlus.Structure")]
        public static object CurveType(string CurveType) { s.CurveType(CurveType); return CurveType; }

        //OTC Instrument templates
        [ExcelFunction(Description = "finplusstructure VanillaSwapTemplate", Category = "FinPlus.Template")]
        public static object VanillaSwapBuild(string Instrument, string CurveName, int SettlementDays, bool EndOfMonth, string Maturity, string Index, double Spread, string FixLegFrq, string FltLegFrq, string FixLegConv, string FltLegConv, string FixLegDayCount, string FltLegDayCount, string Holidays) { s.VanillaSwapBuild(Instrument, CurveName, SettlementDays, EndOfMonth, Maturity, Index, Spread, FixLegFrq, FltLegFrq, FixLegConv, FltLegConv, FixLegDayCount, FltLegDayCount, Holidays); return Instrument; }

        [ExcelFunction(Description = "finplusstructure ForwardRateAgreementBuild", Category = "FinPlus.Template")]
        public static object ForwardRateAgreementBuild(string Instrument, string CurveName, int SettlementDays, string Start, string Maturity, string Index, string DayCount, string BizConv, string Holidays) { s.ForwardRateAgreementBuild(Instrument, CurveName, SettlementDays, Start, Maturity, Index, DayCount, BizConv, Holidays); return Instrument; }

        [ExcelFunction(Description = "finplusstructure DepositLoanBuild", Category = "FinPlus.Template")]
        public static object DepositLoanBuild(string Instrument, string CurveName, int SettlementDays, string Maturity, string Index, string DayCount, string BizConv, string Holidays) { s.DepositLoanBuild(Instrument, CurveName, SettlementDays, Maturity, Index, DayCount, BizConv, Holidays); return Instrument; }

        [ExcelFunction(Description = "finplusstructure ZCMBuild", Category = "FinPlus.Template")]
        public static object ZCMBuild(string Instrument, string Maturity, int SettlementDays, string DayCount, string BizConv, string Holidays) { s.ZCMBuild(Instrument, Maturity, SettlementDays, DayCount, BizConv, Holidays); return Instrument; }

        [ExcelFunction(Description = "finplusstructure IRFutureBuild", Category = "FinPlus.Template")]
        public static object IRFutureBuild(string Instrument, string CurveName, int StartDate, int FutMonths, string DayCount, string BizConv, string Holidays){s.IRFutureBuild(Instrument, CurveName, StartDate, FutMonths, DayCount, BizConv, Holidays); return Instrument; }
 
        [ExcelFunction(Description = "finplusstructure CreditDefaultSwap", Category = "FinPlus.Template")]
        public static object CreditDefaultSwapBuild(string Instrument, string CurveName, int SettlementDays, string Maturity, string PremFreq, string PremConv, string PayConv, string DayCount, string Holidays) { s.CreditDefaultSwapBuild(Instrument, CurveName, SettlementDays, Maturity, PremFreq, PremConv, PayConv, DayCount, Holidays); return Instrument; }

        [ExcelFunction(Description = "finplusstructure YieldCurveBuild", Category = "FinPlus.Template")]
        public static object YieldCurveBuild(string CurveName, string Ccy, int NumDepo, int NumFut, int NumFra, int NumSwap, string Index, string DayCount, string Tolerance, string Holidays) { s.YieldCurveBuild(CurveName, Ccy, NumDepo, NumFut, NumFra, NumSwap, Index, DayCount, Tolerance, Holidays); return CurveName; }

        [ExcelFunction(Description = "finplusstructure DefaultCurveBuild", Category = "FinPlus.Template")]
        public static object DefaultCurveBuild(string CurveName, string Ccy, int NumSpread, string CreditName, double RecoveryRate, string DayCount, string Holidays) { s.DefaultCurveBuild(CurveName, Ccy, NumSpread, CreditName, RecoveryRate, DayCount, Holidays); return CurveName; }

        [ExcelFunction(Description = "finplusstructure BondCurveBuild", Category = "FinPlus.Template")]
        public static object BondCurveBuild(string CurveName, string Ccy, int NumZCM, int NumBond, string DayCount, double Tolerance, string Holidays) { s.BondCurveBuild(CurveName, Ccy, NumZCM, NumBond, DayCount, Tolerance, Holidays); return CurveName; }

        [ExcelFunction(Description = "finplusstructure IndexBuild", Category = "FinPlus.Template")]
        public static object IndexBuild(string IndexName, string CurveName, string IndexType, string Tenor) { s.IndexBuild(IndexName, CurveName, IndexType, Tenor); return IndexName; }

           //business structure
        [ExcelFunction(Description = "finplusstructure Location", Category = "FinPlus.Structure")]
        public static object Location(string Location, string Country, string Region) { s.Location(Location, Country, Region); return Location; }

        [ExcelFunction(Description = "finplusstructure Department", Category = "FinPlus.Structure")]
        public static object Department(string Department, string Location) { s.Department(Department, Location); return Department; }

        [ExcelFunction(Description = "finplusstructure Desk", Category = "FinPlus.Structure")]
        public static object Desk(string Desk, string Department) { s.Desk(Desk, Department); return Desk; }

        [ExcelFunction(Description = "finplusstructure Book", Category = "FinPlus.Structure")]
        public static object Book(string Book, string Desk, string Department, string Location) { s.Book(Book, Desk, Department, Location); return Book; }

        [ExcelFunction(Description = "finplusstructure User", Category = "FinPlus.Admin")]
        public static object User(string User, string UserName, string Desk, string Location) { s.User(User, UserName, Desk, Location); return User; }

        //country structure
        [ExcelFunction(Description = "finplusstructure Region", Category = "FinPlus.Admin")]
        public static object Region(string Region) { s.Region(Region); return Region; }

        [ExcelFunction(Description = "finplusstructure Country", Category = "FinPlus.Admin")]
        public static object Country(string Country, string Region) { s.Country(Country, Region); return Country; }

        //company structure
        [ExcelFunction(Description = "finplusstructure Counterparty", Category = "FinPlus.Structure")]
        public static object Counterparty(string Counterparty, string LongName, string Identifier, string DefaultCollateralCurve, string Country, string Sector, string Region, string InternalRating, string MoodysRating, string SnPRating){ s.Counterparty(Counterparty, LongName, Identifier, DefaultCollateralCurve, Country, Sector, Region, InternalRating, MoodysRating, SnPRating); return Counterparty; }

        [ExcelFunction(Description = "finplusstructure CounterpartyQuoteTier", Category = "FinPlus.Structure")]
        public static object CounterpartyQuoteTier(string Counterparty, string InstrumentType, double Adjustment) { s.CounterpartyQuoteTier(Counterparty, InstrumentType, Adjustment); return Counterparty; }

        [ExcelFunction(Description = "finplusstructure Sector", Category = "FinPlus.Admin")]
        public static object Sector(string Sector) { s.Sector(Sector); return Sector; }

        [ExcelFunction(Description = "finplusstructure InternalRating", Category = "FinPlus.Admin")]
        public static object InternalRating(string InternalRating, int Index) { s.InternalRating(InternalRating, Index); return InternalRating; }

        [ExcelFunction(Description = "finplusstructure MoodysRating", Category = "FinPlus.Admin")]
        public static object MoodysRating(string MoodysRating, int Index) { s.MoodysRating(MoodysRating, Index); return MoodysRating; }

        [ExcelFunction(Description = "finplusstructure SnPRating", Category = "FinPlus.Admin")]
        public static object SnPRating(string SnPRating, int Index) { s.SnPRating(SnPRating, Index); return SnPRating; }

        //admin
        [ExcelFunction(Description = "finplusstructure application info", Category = "FinPlus.Admin")]
        public static object Application(string Application, string Description, string Owner) { s.Application(Application, Description, Owner); return Application; }

        [ExcelFunction(Description = "sets functions to be mapped and creates tables if they do not exist", Category = "FinPlus.StaticData")]
        public static object FunctionMap(string Group, string Name) { s.FunctionMap(Group, Name); return Group; }
        #endregion

        //swig test
        #region swigtest
        [ExcelFunction(Description = "test", Category = "FinPlus.Analytics")]
        public static object SwigTest(string Value)
        {
            try
            {
                return ql.SwigTest(Value);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }
        #endregion
    }
}
